<template>
  <div class="permission-page">
    <el-table :data="dataList" row-key="id">
      <el-table-column v-for="col in columns" :key="col.key" :prop="col.key" :label="col.title" :width="col.width||'auto'">
        <template slot-scope="scope">
          <span v-if="col.title=='资源名称'">
            <d2-icon :name="scope.row.icon||'file-o'"/>
            {{scope.row.name}}
          </span>
          <div v-if="col.title=='状态'">
            <el-tag :type="scope.row.visible ? 'primary' : 'danger'" disable-transitions>{{scope.row.visible?'启用':'停用'}}</el-tag>
          </div>
          <div v-if="col.title=='操作'">
            <el-button v-for="btn in btns" :key="btn.url"
                       size="mini" :icon="btn.icon"
                       :style="{color:btn.url=='del'?'#f56c6c':(btn.url=='add'?'#027be3':'#67c23a')}"
                       @click="custMethod(btn.url,scope.row)"/>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <detail-info ref="detailFrom" v-model="isShowDetail" @submit="initPage"/>
  </div>
</template>
<script>

  import detailInfo from "./detail-info";
  import menuApi from "@/api/system/menu";
  import HeadBtns from "@/views/comps/zhzf/head-btns";
  import utils from '@/assets/utils/zhzf-util'
  import {getMenuByPath} from "@/assets/utils/menu-util";

  export default {
    name: "RolePage",
    components: {HeadBtns, detailInfo},
    data() {
      return {
        btns: [],
        isShowDetail: false,
        columns: [
          {title: '资源名称', key: 'name', width: 400},
          {title: 'URL', key: 'url'},
          {title: '状态', key: 'visible', width: 100},
          {title: '操作', key: 'op', width: 100}
        ],
        allData: [],
        dataList: [],
        selected: [],
      };
    },
    async mounted() {
      await this.pageBtns();
      await this.initPage();
    },
    methods: {
      custMethod(url, data) {
        let cmt = eval('this.' + url);
        cmt(data);
      },
      async pageBtns() {
        let path = this.$route.path;
        let menu = getMenuByPath(path);
        if (!menu) {
          return;
        }
        let res = await menuApi.child({type: 'op', pid: menu.id});
        this.btns = [];
        (res.data || []).forEach(item => {
          if (!item.url || !item.visible) {
            return false;
          }
          this.btns.push(item)
        })
      },
      async initPage() {
        this.loading = true;
        let res = await menuApi.list();
        let data = res.data || [];
        this.allData = data;
        this.allData.sort((a, b) => {
          return a.sort > b.sort ? 1 : -1;
        });
        data = utils.generatorDeptTree(data);
        this.dataList = data.treeList;
        this.loading = false;
      },
      showDetail(row) {
        this.isShowDetail = true;
        this.$refs.detailFrom.initPage(row, this.dataList);
      },
      add(row) {
        this.showDetail({pid: row.id, pname: row.name, visible: 1});
      },
      mod(row) {
        //主菜单没有父节点名称
        if (row.pid >= 1) {
          let pn = this.allData.filter(item => item.id == row.pid);
          row.pname = pn[0].name;
        }
        this.showDetail(row)
      },
      del(row) {
        if (!row && !this.selected.length) {
          utils.showTips('请选择要删除的数据', false);
          return;
        }
        let delIds = [];
        if (row) {
          delIds = [row.id];
        } else {
          this.selected.map(item => {
            delIds.push(item.id)
          });
        }

        this.$confirm("确认删除？", "确认信息", {
          distinguishCancelAndClose: true,
          confirmButtonText: "删除",
          cancelButtonText: "取消"
        }).then(async () => {
          let hasChildren = this.allData.some(item => {
            return delIds.indexOf(item.id) != -1 && item.children.length
          });
          if (hasChildren) {
            this.$confirm("确认删除包含子节点的所有数据？", "确认信息", {
              distinguishCancelAndClose: true,
              confirmButtonText: "删除",
              cancelButtonText: "取消"
            }).then(() => {
              this.submitDel(delIds)
            })
          } else {
            this.submitDel(delIds)
          }
        });
      },
      async submitDel(ids) {
        let res = await menuApi.del(ids);
        await this.initPage();
      }
    }
  };
</script>

<style lang="scss">
  .permission-page {
    height: calc(100% - 15px) !important;

    .el-table {
      height: 100%;
      overflow: auto;

      .cell {
        padding-left: 10px !important;
      }

      .el-table__expand-icon {
        padding-top: 2px;
        float: left;
      }
    }
  }
</style>
